package com.cskaoyan.service.user;

import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.PageDataVo;
import com.cskaoyan.bean.user.*;
import com.cskaoyan.mapper.user.*;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    AddressMapper addressMapper;
    @Autowired
    CollectMapper collectMapper;
    @Autowired
    FootprintMapper footprintMapper;
    @Autowired
    HistoryMapper historyMapper;
    @Autowired
    FeedbackMapper feedbackMapper;

    @Override
    public PageDataVo selectUser(String username, String mobile, BaseParam baseParam) {
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        if (username != null && !"".equals(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        if (mobile != null && !"".equals(mobile)) {
            criteria.andMobileEqualTo(mobile);
        }
        userExample.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());
        List<User> users = userMapper.selectByExample(userExample);
        return PageDataVo.ok(users);
    }

    @Override
    public User selectUserById(Integer paramId) {
        User user = userMapper.selectByPrimaryKey(paramId);
        return user;
    }

    @Override
    public PageDataVo listAddress(String name, Integer userId, BaseParam param) {
        PageHelper.startPage(param.getPage(), param.getLimit());
        AddressExample addressExample = new AddressExample();
        AddressExample.Criteria criteria = addressExample.createCriteria();
        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (userId != null && !"".equals(userId)) {
            criteria.andUserIdEqualTo(userId);
        }
        addressExample.setOrderByClause(param.getSort() + " " + param.getOrder());
        List<Address> addresses = addressMapper.selectByExample(addressExample);
        return PageDataVo.ok(addresses);
    }

    @Override
    public PageDataVo listCollect(Integer userId, Integer valueId, BaseParam param) {
        PageHelper.startPage(param.getPage(), param.getLimit());
        CollectExample collectExample = new CollectExample();
        CollectExample.Criteria criteria = collectExample.createCriteria();
        if (userId != null && !"".equals(userId)) {
            criteria.andUserIdEqualTo(userId);
        }
        if (valueId != null && !"".equals(valueId)) {
            criteria.andValueIdEqualTo(valueId);
        }
        collectExample.setOrderByClause(param.getSort() + " " + param.getOrder());
        List<Collect> collects = collectMapper.selectByExample(collectExample);
        return PageDataVo.ok(collects);
    }

    @Override
    public PageDataVo listFootprint(Integer userId, Integer goodsId, BaseParam baseParam) {
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        FootprintExample footprintExample = new FootprintExample();
        FootprintExample.Criteria criteria = footprintExample.createCriteria();
        if (userId != null && !"".equals(userId)) {
            criteria.andUserIdEqualTo(userId);
        }
        if (goodsId != null && !"".equals(goodsId)) {
            criteria.andGoodsIdEqualTo(goodsId);
        }
        footprintExample.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());
        List<Footprint> footprints = footprintMapper.selectByExample(footprintExample);
        return PageDataVo.ok(footprints);
    }

    @Override
    public PageDataVo listHistory(Integer userId, String keyword, BaseParam baseParam) {
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        HistoryExample historyExample = new HistoryExample();
        HistoryExample.Criteria criteria = historyExample.createCriteria();
        if (userId != null && !"".equals(userId)) {
            criteria.andUserIdEqualTo(userId);
        }
        if (!"".equals(keyword) && null != keyword) {
            criteria.andKeywordLike("%" + keyword + "%");
        }
        historyExample.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());
        List<History> histories = historyMapper.selectByExample(historyExample);
        return PageDataVo.ok(histories);
    }

    @Override
    public PageDataVo listFeedback(String userName, Integer feedbackId, BaseParam baseParam) {
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        FeedbackExample feedbackExample = new FeedbackExample();
        FeedbackExample.Criteria criteria = feedbackExample.createCriteria();
        if (null != userName && !"".equals(userName)) {
            criteria.andUsernameLike("%" + userName + "%");
        }
        if (null != feedbackId && !"".equals(feedbackId)) {
            criteria.andIdEqualTo(feedbackId);
        }
        feedbackExample.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());
        List<Feedback> feedbacks = feedbackMapper.selectByExample(feedbackExample);
        return PageDataVo.ok(feedbacks);
    }

    @Override
    public void updateUser(User user) {
        userMapper.updateByPrimaryKeySelective(user);
    }
}
